﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace ASoft.Data
{
    [DataContract]
    public class UserData
    {
        [DataMember]
        public string Code { get; set; }

        [DataMember]
        public string Name { get; set; }


        [DataMember]
        public bool IsAdmin { get; set; }

        public string UserTypeName
        {
            get
            {
                if (IsAdmin)
                {
                    return "管理员";
                }
                return "普通用户";

            }
        }


        [DataMember]
        string _Password { get; set; }

        public bool ValidatePassword(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                return false;
            }
            if (string.IsNullOrWhiteSpace(_Password))
            {
                return false;
            }
            return AppUtility.GetHash(password) == _Password;
        }

        public void ChangedPassword(string oldPassword, string newPassword, string secondPassword)
        {
            if (!string.Equals(secondPassword, newPassword))
            {
                throw new Exception("二次输入密码不一致");
            }
            if (!ValidatePassword(oldPassword))
            {
                throw new Exception("原始密码错误");
            }
            ResetPassword(newPassword);
        }

        public void ResetPassword(string newPassword)
        {
            if (string.IsNullOrWhiteSpace(newPassword))
            {
                throw new Exception("密码不能为空");
            }
            this._Password = AppUtility.GetHash(newPassword);
            RC.Current.SaveRootData();
        }
    }
}
